import { requestClient } from '#/api/request'
import { treeToList } from '#/helper/treeHelper'
import { localesMap } from '#/locales'
import { toLocalesTreeOptions } from '#/utils/localesConfig'
import type { ApiPaginationParams } from '#types/utils'

/**
 * 字典子级列表接口
 */
export default class DictionarySubApi {
    static get inst() {
        return DictionarySubApi._inst || (DictionarySubApi._inst = new DictionarySubApi())
    }
    private static _inst: DictionarySubApi

    /**
     * 新增
     * @param params
     */
    add(params: Partial<any>) {
        return requestClient.post<null>('/dictdata/add', params)
    }

    /**
     * 批量删除
     * @param ids
     */
    delete(ids: Array<number | string>) {
        return requestClient.delete<null>(`/dictdata/delete/${ids}`)
    }
    /**
     * 编辑
     * @param id
     * @param params
     */
    edit(id: number | string, params: Partial<any>) {
        return requestClient.put<null>(`/dictdata/edit`, {
            ...params,
            id
        })
    }

    /**
     * 获取字典下的字列表
     */
    async getPageList(
        params: ApiPaginationParams<{
            dictId?: number | string
        }>
    ) {
        // eslint-disable-next-line unicorn/no-await-expression-member
        const zh = treeToList(toLocalesTreeOptions((await localesMap['zh-CN']?.())?.default || []))
        // eslint-disable-next-line unicorn/no-await-expression-member
        const en = treeToList(toLocalesTreeOptions((await localesMap['en-US']?.())?.default || []))

        return {
            total: 2,
            records: [
                {
                    name: zh.find((v: any) => v.value === params.dictId)?.label || '',
                    id: 11,
                    createUser: 1,
                    createTime: '2025-03-16 08:10:28',
                    updateUser: 1,
                    updateTime: '2025-03-17 09:57:38',
                    mark: 1,

                    code: 'CHN',
                    dictId: 4,
                    status: 1,
                    note: '',
                    sort: 1
                },
                {
                    name: en.find((v: any) => v.value === params.dictId)?.label || '',
                    id: 12,
                    createUser: 1,
                    createTime: '2025-03-16 08:11:21',
                    updateUser: 1,
                    updateTime: '2025-03-17 09:57:47',
                    mark: 1,

                    code: 'USA',
                    dictId: 4,
                    status: 1,
                    note: '',
                    sort: 2
                }
            ],
            current: 1,
            pages: 1,
            size: 20
        }

        return requestClient.get<any>('/dictdata/index', {
            params
        })
    }
}
